package com.scgl.factory.service.impl;

import com.scgl.factory.domain.Factory;
import com.scgl.factory.mapper.FactoryMapper;
import com.scgl.factory.service.IFactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author mobai
 * @date 2024-10-24
 */
@Service
public class FactoryServiceImpl implements IFactoryService
{
    @Autowired
    private FactoryMapper factoryMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Factory selectFactoryById(Long id)
    {
        return factoryMapper.selectFactoryById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param factory 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Factory> selectFactoryList(Factory factory)
    {
        return factoryMapper.selectFactoryList(factory);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param factory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertFactory(Factory factory)
    {
        return factoryMapper.insertFactory(factory);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param factory 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateFactory(Factory factory)
    {
        return factoryMapper.updateFactory(factory);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteFactoryByIds(Long[] ids)
    {
        return factoryMapper.deleteFactoryByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteFactoryById(Long id)
    {
        return factoryMapper.deleteFactoryById(id);
    }

    @Override
    public List<Factory> selectFactoryLinkageList(Long id) {
        return factoryMapper.selectFactoryLinkageList(id);
    }

    @Override
    public List<Factory> buildFactoryTree() {
        List<Factory> factories = factoryMapper.selectFactoryList(null);
        List<Factory> result = buildTree(factories);
        return result;
    }

    private List<Factory> buildTree(List<Factory> factories) {
        List<Factory> factoryArrayList = new ArrayList<>();
        for (Factory factory : factories) {
            if (factory.getLinkage() == 0) {
                factoryArrayList.add(tryAddChildren(factory, factories));
            }
        }
        return factoryArrayList;
    }

    private Factory tryAddChildren(Factory factory, List<Factory> factories) {
        factory.setChildren(new ArrayList<>());
        factory.setLabel(factory.getName());
        for (Factory child : factories) {
            if (child.getLinkage().intValue() == factory.getId().intValue()) {
                factory.getChildren().add(tryAddChildren(child, factories));
            }
        }
        return factory;
    }
}
